﻿using CloudMusic.Helper;
using CloudMusic.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CloudMusic.UC
{
    /// <summary>
    /// UCRadioLable.xaml 的交互逻辑
    /// </summary>
    public partial class UCRadioLable : UserControl
    {
        private double index;//标签每次移动的长度
        private int lbpage;//标签总页数
        private int laPage = 1;//标签滑动时候当前的页数
        public UCRadioLable()
        {
            InitializeComponent();
            toLeft.Visibility = Visibility.Collapsed;
            //GetDJLable();
            //this.SizeChanged += MainWindow_Resize;
        }

        #region 主播电台
        private void MainWindow_Resize(object sender, EventArgs e)
        {
            if (this.ActualWidth > 850)
            {
                for (int i = 0; i < lableBtn.Children.Count; i++)
                {
                    Thickness lb = new Thickness();
                    lb.Left = 42;
                    lableBtn.Children[i].SetValue(MarginProperty, lb);
                }
               index = cv.ActualWidth* laPage;
            }
            else
            {
                for (int i = 0; i < lableBtn.Children.Count; i++)
                {
                    Thickness lb = new Thickness();
                    lb.Left = 12;
                    lableBtn.Children[i].SetValue(MarginProperty, lb);
                }
                index = cv.ActualWidth* laPage;
            }
        }
        /// <summary>
        /// 获取电台的标签
        /// </summary>
        public void GetDJLable()
        {
            Button bt = default;
            var list = CommonHelper.GetEquipList<DJLableModel>(1, "http://localhost:3000/dj/catelist");
            for (int i = 0; i < list.categories.Count; i++)
            {
                //DJLableButtonStyle
                bt = new Button { Style = TryFindResource("DJLableButtonStyle") as Style, DataContext = list.categories[i] };
                bt.Tag = list.categories[i].id;
                //bt.Click += new RoutedEventHandler(Btn_Click);
                //Thickness th = new Thickness();
                //th.Left = 12;
                //bt.Margin = th;
                lableBtn.Children.Add(bt);
            }
            //在第一个插入（排行榜）
            bt = new Button { Style = TryFindResource("DJLablePHBtn") as Style };
            lableBtn.Children.Insert(0, bt);
            //最后一个插入（我要做主播）
            bt = new Button { Style = TryFindResource("DJLableZBBtn") as Style };
            lableBtn.Children.Insert(lableBtn.Children.Count, bt);
            //页数为多少
            lbpage = Convert.ToInt16(Math.Ceiling((double)lableBtn.Children.Count / 9));
        }
        /// <summary>
        /// 左移动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToLeft_Click(object sender, RoutedEventArgs e)
        {
            laPage--;
            index -= cv.ActualWidth;
            if (laPage == 1)
            {
                toLeft.Visibility = Visibility.Collapsed;
                laPage = 1;
            }
            toRight.Visibility = Visibility.Visible;
            var animation = new DoubleAnimation()
            {
                From = -index,
                To = -index + cv.ActualWidth,
                SpeedRatio = 8,
            };
            myTranslateTransform.BeginAnimation(TranslateTransform.XProperty, animation);
          
        }
        /// <summary>
        /// 右移动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToRight_Click(object sender, RoutedEventArgs e)
        {
            laPage++;
            if (laPage == lbpage)
            {
                toRight.Visibility = Visibility.Collapsed;
                laPage = lbpage;
            }
            toLeft.Visibility = Visibility.Visible;
            var animation = new DoubleAnimation()
            {
                From = -index + cv.ActualWidth,
                To = -index,
                SpeedRatio = 8,
            };
            myTranslateTransform.BeginAnimation(TranslateTransform.XProperty, animation);
            index += cv.ActualWidth;
        }
        #endregion
    }
}
